#pragma once
#include <memory>
#include <sstream>
#include <jsoncpp/json/json.h>
namespace leyi_media
{
    namespace json_util
    {
        using namespace Json;
        using namespace std;
        // pair<bool,string>Serialize(const Value&root) or bool Serialize(const Value &root, string &str)
        bool Serialize(const Value &root, string &str)
        {
            // writer bulider
            unique_ptr<StreamWriter> p_writer(StreamWriterBuilder().newStreamWriter());
            // string stream to recive the serizlized string
            stringstream str_stream;
            if (p_writer->write(root, &str_stream))
                return false;
            // take out res out of string_stream
            str.append(move(str_stream.str()));
            return true;
        }
        bool Unserialize(Value &root, const string &str)
        {
            // reader bulider
            unique_ptr<CharReader> p_read(CharReaderBuilder().newCharReader());
            // string stream
            stringstream str_stream;
            // error string
            string error;
            // parse
            if (p_read->parse(str.c_str(), str.c_str() + str.size(), &root, &error))
                return true;
            return false;
        }
    }
}
